package hts.project.controller;

import hts.project.service.UserService;
import hts.project.vo.AddressVO;
import hts.project.vo.MemberVO;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UsersController {

	@Autowired
	private UserService userService;
	
	@RequestMapping(value="main.do",method = RequestMethod.GET)
	public ModelAndView main() {
		ModelAndView mav = new ModelAndView();
		
		mav.setViewName("01_M001");
		return mav;
	}
		
	@RequestMapping("login.do")
	public ModelAndView login(){

		ModelAndView mav = new ModelAndView();
		mav.setViewName("users/03_L001");
		return mav;
	}
	
	@RequestMapping(value="login_ok.do" ,method = RequestMethod.POST)
	public String login(@RequestParam("id") String user_id, @RequestParam("pwd")String pwd, HttpSession session, HttpServletRequest request,HttpServletResponse response) throws Exception {
		MemberVO loginUser = userService.login(user_id);
		
		if (loginUser != null) {
			
			session.setAttribute("user", loginUser);
			if(loginUser.getPwd().equals(pwd)){
				String id=loginUser.getUser_id();
				session.setAttribute("id",id);
				return "01_M001";
			}
			else{
				PrintWriter out = response.getWriter();
				out.println("<script>");
				out.println("alert('비밀번호가 잘못되었습니다.');");
				out.println("history.back();");
				out.println("</script>");
				return null;
			}
		} else {
			return "users/03_L001";
		}
	}
	
	@RequestMapping("join.do")
	public ModelAndView join(){

		ModelAndView mav = new ModelAndView();
		mav.setViewName("users/12_J001");
		return mav;
	}
	
	@RequestMapping(value="join_ok.do",method=RequestMethod.POST)
		public ModelAndView join_ok ( HttpServletRequest request, 
					HttpServletResponse response) throws IOException{
			
			System.out.println("join ok로 넘어옴");
			// id 받기
			String user_id = request.getParameter("user_id");
			String pwd = request.getParameter("pwd");
			String name = request.getParameter("name");
			System.out.println(user_id+"/"+pwd+"/"+name+"/");
			System.out.println("폰 삽입시도");
			String phone=request.getParameter("phone");
			System.out.println("폰 삽입성공");
			String acc_question=request.getParameter("acc_question");
			String acc_answer=request.getParameter("acc_answer");	
			
			String name2=request.getParameter("name2");
			String phone2=request.getParameter("phone2");
			
			if(name2==null){
				name2="";
			}
			if(phone2==null){
				phone2="";
			}
			
			System.out.println("phone:"+phone);
			System.out.println(user_id+"/"+pwd+"/"+name+"/"+name2+"/"+phone+"/"+acc_question+"/"+acc_answer);
		

			MemberVO vo = new MemberVO();
			vo.setUser_id(user_id);  
			vo.setPwd(pwd); 
			vo.setName(name);
			vo.setName2(name2); 
			vo.setPhone(phone);
			vo.setPhone2(phone2);
			vo.setAcc_question(acc_question);
			vo.setAcc_answer(acc_answer); 
			
			
			//session.setAttribute("address", address);
			String addr01=request.getParameter("addr01");
			String addrLoad=request.getParameter("roadAddress01");
			String addrDetail = request.getParameter("addr_detail01");
			String addrFull = addr01+addrDetail;
			
			
		/*	//test
			String addr01="서울시 강동구 강일동";
			String addrDetail = " 강일리버파크";
			String addrFull = addr01+addrDetail;
		*/	
			
			AddressVO addr = new AddressVO();
			System.out.println("addr :"+addrFull);
		/*	add.setAddr_id(addr_id);*/
			addr.setUser_id(user_id);
			addr.setAddr(addrFull);
		
			// vo 객체를 실인수로 메서드 호출
			this.userService.insert(vo); // 레코드 삽입
			System.out.println("vo 입력되쓰");
			this.userService.insertAddr(addr);//레코드 삽입.
			System.out.println("확인페이지로 이동");
			response.sendRedirect("join_confirm.do"); // 전체목록
			System.out.println("이동해쓰");
			return null;
		}
	
	
	@RequestMapping("join_confirm.do")
	public ModelAndView join_confirm(){

	ModelAndView mav = new ModelAndView();
	mav.setViewName("users/13_J002");
			
	return mav;
	}
	
	
	@RequestMapping("selectAllUser.do")
	public ModelAndView selectAll() 
	{
		List<MemberVO> list = this.userService.getAllUsers();
		
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("list", list);
		
		mav.setViewName("userList");
	
		return mav;
	
	}
	
	@RequestMapping("fpwd.do")
	public ModelAndView findpwd(){
	ModelAndView mav = new ModelAndView();
		
		mav.setViewName("users/14_L101");

		return mav;
	}
	
	@RequestMapping(value="fpwd_ok.do", method = RequestMethod.POST)
	public String findpwd_ok(@RequestParam("id") String user_id, @RequestParam("question") String acc_question, @RequestParam("answer")String acc_answer, 
				HttpSession session, HttpServletRequest request,HttpServletResponse response) throws IOException{
		MemberVO loginUser = userService.login(user_id);
	
		if (loginUser != null) {
			String db_id = (String)loginUser.getUser_id();
			String db_qus=(String)loginUser.getAcc_question();
			String db_anws=(String)loginUser.getAcc_answer();
			
			if(acc_question.equals(db_qus)&&acc_answer.equals(db_anws)){
				session.setAttribute("user_id", user_id);
				session.setAttribute("user", loginUser);
				return "users/14_L102";
			}
			else{
				PrintWriter out = response.getWriter();
				out.println("<script>");
				out.println("alert('입력하신 정보를 찾을 수 없습니다.');");
				out.println("history.back();");
				out.println("</script>");
				return null;			
				}
		} else {
			
			PrintWriter out = response.getWriter();
			out.println("<script>");
			out.println("alert('입력하신 id를 찾을 수 없습니다.');");
			out.println("history.back();");
			out.println("</script>");
			return "users/03_L001";
		}
	}
	/*@RequestMapping(value="/editAccount", method=RequestMethod.GET)
	public String editAccount() {
		return "users/editAccount";
	}
	
	
	@RequestMapping(value="/editAccount", method=RequestMethod.POST)
	public String editAccount(MemberVO user, HttpSession session) throws Exception {
		MemberVO loginUser = (MemberVO) session.getAttribute("user");
		String user_id = loginUser.getUser_id(); //세션에 저장된 사용자 정보로부터 이메일을 알아낸다.
		
		if (user.getName() == null) {
			user.setName(loginUser.getName());
		}
		if (user.getPhone() == null) {
			user.setPhone(loginUser.getPhone());
		}
		
		user.setUser_id(user_id);
		int check = userService.modify(user);
		if (check == 1) {
			session.setAttribute("user",user);
		}
		
		return "users/changePasswd";
		
	}
	
	*/
	
	@RequestMapping(value="changePasswd.do")
	public String changePasswd() {
		return "users/changePasswd";
	}
	
	@RequestMapping(value="changePasswd_ok.do", method=RequestMethod.POST)
	public void changePwd(@RequestParam("pwd") String pwd, @RequestParam("pwd_chk") String pwd_chk,HttpSession session, HttpServletResponse response) throws Exception {
		MemberVO loginUser = (MemberVO)session.getAttribute("user");
		
		// 출력 및 기타 사항
		PrintWriter out= response.getWriter();
		String user_id;
		
		// 비밀번호 확인
		if(pwd.equals(pwd_chk))
		{
			user_id = (String)session.getAttribute("user_id");
			// 비밀번호 수정 위한 객체 요소 수정 
			loginUser.setPwd(pwd);
			// Update 쪽으로 보내기~~
			userService.changePwd(loginUser);
			
		}
		
		out.println("<script>");
		out.println("window.close()");
		out.println("</script>");
	}
	
	@RequestMapping(value="mypage.do")
	public ModelAndView OrderList(HttpSession session){
		ModelAndView mav = new ModelAndView();
		session.getAttribute("id");
		mav.setViewName("mypage/m_orderlist");
		return mav;
	}
	/*
	@RequestMapping(value="/bye", method=RequestMethod.GET)
	public String bye(HttpSession session) {
		MemberVO user = (MemberVO)session.getAttribute("user");
		userService.logout(user);
		session.invalidate();
		
		return "users/login";
	}
	
	*/
	
	@RequestMapping(value="logout.do", method=RequestMethod.GET)
	public String logout(HttpSession session) {
		session.removeAttribute("id");
		session.removeAttribute("user");
		session.invalidate();
		
		return "01_M001";

	}
}
